import axios from 'axios'
import { message } from 'ant-design-vue'

/**
 * 处理错误信息
 * status:状态吗
 * info:具体信息
 */
const errorHandle = (status, info) => {
  switch (status) {
    case 400:
      message.error('语义错误')
      break
    case 401:
      message.error('服务器认证失败')
      break
    case 403:
      message.error('服务器请求拒绝执行')
      break
    case 404:
      message.error('请检查网路请求地址')
      break
    case 500:
      message.error('服务器发生意外')
      break
    case 502:
      message.error('服务器无响应')
      break
    default:
      message.error(info)
      break
  }
}

/**
 * 创建Axios对象
 */
const instance = axios.create({
  baseURL: 'http://192.168.10.6:8102',
  // timeout: 6000,
  // headers: {
  //   'Content-Type': 'application/json'
  // }
})

/**
 * 拦截器
 */
instance.interceptors.request.use(
  config => {
    return config
  },
  error => Promise.reject(error)
)

instance.interceptors.response.use(
  response => (response.status === 200 ? Promise.resolve(response) : Promise.reject(response)),
  error => {
    const { response } = error
    if (response) {
      errorHandle(response.status, response.info)
    } else {
      message.error('网络请求被中断了')
    }
  }
)

export default instance    